﻿<ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

	xmlns:ThemeBehav="clr-namespace:Themes.Behaviour"

	xmlns:local="clr-namespace:UnitComboLib;assembly=UnitComboLib"
  xmlns:behav="clr-namespace:UnitComboLib.Behaviour;assembly=UnitComboLib"
  >
  <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="../Brushes.xaml" />
  </ResourceDictionary.MergedDictionaries>

  <!-- Fill Brushes -->

  <!-- This Brush is used to highlight the ToggleButton on MouseOver -->
  <LinearGradientBrush x:Key="UnitCmbDarkBrush" StartPoint="0,0" EndPoint="0,1">
    <GradientBrush.GradientStops>
      <GradientStopCollection>
        <GradientStop Color="#FFF" Offset="0.0"/>
        <GradientStop Color="#AAA" Offset="1.0"/>
      </GradientStopCollection>
    </GradientBrush.GradientStops>
  </LinearGradientBrush>

  <SolidColorBrush x:Key="UnitCmbHighlighColorBrush" Color="#AAA" />

  <!-- - VS 2010 -
  <SolidColorBrush x:Key="UnitCmbHighlighColorBrush" Color="#FFe5c365" />
  
  <LinearGradientBrush x:Key="UnitCmbDarkBrush" StartPoint="0,0" EndPoint="0,1">
    <GradientBrush.GradientStops>
      <GradientStopCollection>
        <GradientStop Color="#FFFFFFFF" Offset="0.0"/>
        <GradientStop Color="#FFffefc1" Offset="5.0"/>
        <GradientStop Color="#FFffecb5" Offset="1.0"/>
      </GradientStopCollection>
    </GradientBrush.GradientStops>
  </LinearGradientBrush -->

  <LinearGradientBrush x:Key="UnitCmbPressedBrush" StartPoint="0,0" EndPoint="0,1">
    <GradientBrush.GradientStops>
      <GradientStopCollection>
        <GradientStop Color="#BBB" Offset="0.0"/>
        <GradientStop Color="#EEE" Offset="0.1"/>
        <GradientStop Color="#EEE" Offset="0.9"/>
        <GradientStop Color="#FFF" Offset="1.0"/>
      </GradientStopCollection>
    </GradientBrush.GradientStops>
  </LinearGradientBrush>

  <SolidColorBrush x:Key="UnitCmbDisabledForegroundBrush" Color="#888" />

  <SolidColorBrush x:Key="UnitCmbDisabledBackgroundBrush" Color="#EEE" />

  <SolidColorBrush x:Key="UnitCmbWindowBackgroundBrush" Color="#FFF" />

  <!-- Border Brushes -->

  <LinearGradientBrush x:Key="UnitCmbNormalBorderBrush" StartPoint="0,0" EndPoint="0,1">
    <GradientBrush.GradientStops>
      <GradientStopCollection>
        <GradientStop Color="#CCC" Offset="0.0"/>
        <GradientStop Color="#444" Offset="1.0"/>
      </GradientStopCollection>
    </GradientBrush.GradientStops>
  </LinearGradientBrush>

  <SolidColorBrush x:Key="UnitCmbDisabledBorderBrush" Color="#AAA" />

  <SolidColorBrush x:Key="UnitCmbSolidBorderBrush" Color="#888" />

  <Style TargetType="{x:Type local:UnitCombobox}">
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <!-- Setter Property="OverridesDefaultStyle" Value="true"/ Not sure whether this is required as it causes an error -->
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
    <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
    <Setter Property="BorderBrush" Value="Beige" />
    <Setter Property="BorderThickness" Value="2" />
    <Setter Property="Background" Value="Black" />
    <!-- Setter Property="MinWidth" Value="120"/ -->
    <!-- Setter Property="MinHeight" Value="20"/ -->
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type local:UnitCombobox}">
          <ControlTemplate.Resources>
            <ControlTemplate x:Key="UnitCmbComboBoxToggleButton" TargetType="ToggleButton">
              <Grid>
                <Grid.ColumnDefinitions>
                  <ColumnDefinition />
                  <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <!-- CornerRadius="2" -->
                <Border x:Name="Border" 
                        Grid.ColumnSpan="2"
                        Background="{DynamicResource ControlBackgroundBrush}"
                        BorderBrush="{DynamicResource NormalBorderBrush}"
                        BorderThickness="0" />
                <Border  Margin="2,0" Grid.Column="1" Background="Transparent" BorderBrush="Transparent">
                  <Path x:Name="Arrow"
                        Fill="{DynamicResource GlyphBrush}"
                        HorizontalAlignment="Center"
                        VerticalAlignment="Center"
                        Data="M 0 0 L 4 4 L 8 0 Z"/>
                </Border>
              </Grid>
              <ControlTemplate.Triggers>
                <Trigger Property="ToggleButton.IsMouseOver" Value="true">
                  <Setter TargetName="Border" Property="Background" Value="{DynamicResource HoverShineBrush}" />
                </Trigger>
                <Trigger Property="ToggleButton.IsChecked" Value="true">
                  <Setter TargetName="Border" Property="Background" Value="{DynamicResource PressedBrush}" />
                </Trigger>
                <Trigger Property="IsEnabled" Value="False">
                  <Setter Property="Background" Value="{StaticResource UnitCmbDisabledBackgroundBrush}" />
                  <Setter Property="BorderBrush" Value="{StaticResource UnitCmbDisabledBorderBrush}" />
                  <Setter Property="Foreground" Value="{StaticResource UnitCmbDisabledForegroundBrush}"/>
                  <Setter TargetName="Arrow" Property="Fill" Value="{StaticResource UnitCmbDisabledForegroundBrush}" />
                </Trigger>
              </ControlTemplate.Triggers>
            </ControlTemplate>

            <!-- ControlTemplate TargetType="TextBox">
              <Border x:Name="PART_ContentHost" Focusable="False"
            Background="{TemplateBinding Background}" />
            </ControlTemplate -->
          </ControlTemplate.Resources>

          <Border x:Name="ControlBorder"
                  Background="{TemplateBinding Background}"
							    BorderBrush="{TemplateBinding BorderBrush}"
							    BorderThickness="{TemplateBinding BorderThickness}">
            <Grid>
              <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
              </Grid.ColumnDefinitions>

              <!-- Margin="3,3,23,3" -->
              <ContentPresenter Name="ContentSite"
                                IsHitTestVisible="False" 
                                Content="{TemplateBinding SelectionBoxItem}"
                                ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                                ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                                
                                VerticalAlignment="Center"
                                HorizontalAlignment="Left"
                                Grid.Column="0" />

              <!-- Template="{StaticResource UnitComboBoxTextBox}" -->
              <TextBox x:Name="PART_EditableTextBox"
                       HorizontalAlignment="Stretch" HorizontalContentAlignment="Right"
                       VerticalAlignment="Center" VerticalContentAlignment="Center"
                       Margin="1,0"
                       Focusable="True"
                       Visibility="Hidden"
                       MaxLength="6"
                       IsReadOnly="{TemplateBinding IsReadOnly}" Grid.Column="0" />

              <Grid Grid.Column="1">
                <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="Auto" />
                  <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>

                <Label Grid.Column="0" Grid.Row="1"
                       Padding="0"
                       Margin="1,0,3,0"
                       BorderThickness="0"
                       ToolTip="{Binding SelectedItem.DisplayNameLong}"
                       HorizontalAlignment="Left"
                       VerticalAlignment="Center" VerticalContentAlignment="Center"
                       behav:ContextMenuBehaviour.MenuList="{Binding ElementName=contextMenuOnUniLabel1}">
                  <Label.ContextMenu>

                    <ContextMenu Name="contextMenuOnUniLabel1" ItemsSource="{Binding Path=UnitList}" Placement="Top">
                      <ContextMenu.ItemContainerStyle>
                        <Style TargetType="{x:Type MenuItem}" >
                          <Setter Property="Command" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Path=DataContext.SetSelectedItemCommand}"/>
                          <Setter Property="CommandParameter" Value="{Binding Key}"/>
                          <Setter Property="Header" Value="{Binding DisplayNameLongWithShort}" />

                          <!-- http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/63696841-0358-4f7a-abe1-e6062518e3d6 -->
                          <Setter Property="ThemeBehav:MergeStyleBehaviour.AutoMergeStyle" Value="True"/>
                        </Style>
                      </ContextMenu.ItemContainerStyle>
                    </ContextMenu>

                  </Label.ContextMenu>

                  <TextBlock Text="{Binding SelectedItem.DisplayNameShort}" Name="LabelTextBlock">
                  <TextBlock.Style>
                    <Style TargetType="{x:Type TextBlock}">
                      <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="true">
                          <Setter Property="TextDecorations" Value="Underline" />
                          <Setter Property="Cursor" Value="Hand" />
                        </Trigger>
                        </Style.Triggers>
                      </Style>
                    </TextBlock.Style>
                  </TextBlock>

                </Label>

                <ToggleButton Name="ToggleButton"
                              Template="{StaticResource UnitCmbComboBoxToggleButton}"
                              Margin="0"
                              Padding="0"
                              Grid.Column="1"
                              Focusable="false"
                              VerticalAlignment="Stretch"
                              IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
                              ClickMode="Press">
                </ToggleButton>

              </Grid>

              <Popup Name="Popup"
                     Placement="Bottom"
                     IsOpen="{TemplateBinding IsDropDownOpen}"
                     AllowsTransparency="False" 
                     Focusable="False"
                     PopupAnimation="Slide">

                <Grid Name="DropDown"
                      SnapsToDevicePixels="True"                
                      MinWidth="{TemplateBinding ActualWidth}"
                      MaxHeight="{TemplateBinding MaxDropDownHeight}">
                    <Border x:Name="DropDownBorder"
                            Background="{DynamicResource PopupWindowBackgroundBrush}"
                            BorderThickness="1"
                            BorderBrush="{DynamicResource SolidBorderBrush}"
                          />

                  <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True">
                    <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
                  </ScrollViewer>
                </Grid>
              </Popup>

            </Grid>
          </Border>

          <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
              <Setter TargetName="ControlBorder" Property="BorderBrush" Value="{StaticResource UnitCmbHighlighColorBrush}" />
              <Setter TargetName="ToggleButton" Property="Background" Value="{StaticResource UnitCmbDarkBrush}" />
            </Trigger>

            <Trigger Property="HasItems" Value="false">
              <Setter TargetName="DropDownBorder" Property="MinHeight" Value="95"/>
            </Trigger>
            <Trigger Property="IsEnabled" Value="false">
              <Setter Property="Foreground" Value="{StaticResource UnitCmbDisabledForegroundBrush}"/>
            </Trigger>
            <Trigger Property="IsGrouping" Value="true">
              <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
            </Trigger>
            <!-- Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="true">
              <Setter TargetName="DropDownBorder" Property="CornerRadius" Value="4"/>
              <Setter TargetName="DropDownBorder" Property="Margin" Value="0,2,0,0"/>
            </Trigger -->
            <Trigger Property="IsEditable"
               Value="true">
              <Setter Property="IsTabStop" Value="false"/>
              <Setter TargetName="PART_EditableTextBox" Property="Visibility"    Value="Visible"/>
              <Setter TargetName="ContentSite" Property="Visibility" Value="Hidden"/>
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

</ResourceDictionary>
